﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
#include "base64worker.h"

Base64Worker::Base64Worker(QObject *parent) : QThread(parent)
{
}

void Base64Worker::setFileName(const QString &fileName)
{
    m_fileName = fileName;
}

void Base64Worker::setTotalSize(qint64 totalSize)
{
    m_totalSize = totalSize;
}

void Base64Worker::setOutputFile(QFile *outputFile)
{
    m_outputFile = outputFile;
}

void Base64Worker::run()
{
    QFile inputFile(m_fileName);
    if (!inputFile.open(QIODevice::ReadOnly)) {
        emit errorOccurred(tr("无法打开输入文件 %1").arg(m_fileName));
        emit done();
        return;
    }

    qint64 processedSize = 0;
    const qint64 blockSize = 1024 * 1024; // 每次读取1MB数据
    QByteArray buffer;

    while (!inputFile.atEnd()) {
        buffer = inputFile.read(blockSize);
        if (!buffer.isEmpty()) {
            QByteArray base64Data = buffer.toBase64();
            m_outputFile->write(base64Data);
            processedSize += buffer.size();
            emit progressChanged(processedSize);
        }
    }

    inputFile.close();
    m_outputFile->close();
    emit done();
}
